package cn.iocoder.yudao.module.team.controller.admin.ordepayment;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.team.controller.admin.ordepayment.vo.*;
import cn.iocoder.yudao.module.team.dal.dataobject.ordepayment.OrdePaymentDO;
import cn.iocoder.yudao.module.team.service.ordepayment.OrdePaymentService;

@Tag(name = "管理后台 - 团款项目")
@RestController
@RequestMapping("/team/orde-payment")
@Validated
public class OrdePaymentController {

    @Resource
    private OrdePaymentService ordePaymentService;

    @PostMapping("/create")
    @Operation(summary = "创建团款项目")
    @PreAuthorize("@ss.hasPermission('team:orde-payment:create')")
    public CommonResult<Integer> createOrdePayment(@Valid @RequestBody OrdePaymentSaveReqVO createReqVO) {
        return success(ordePaymentService.createOrdePayment(createReqVO));
    }
    @PostMapping("/createBatch")
    @Operation(summary = "批量创建团款项目")
    @PreAuthorize("@ss.hasPermission('team:orde-payment:createBatch')")
    public CommonResult<Boolean> createBatchOrdePayment(@Valid @RequestBody List<OrdePaymentDO> createReqVO) {
        ordePaymentService.createBatchOrdePayment(createReqVO);
        return success(true);
    }

    @PutMapping("/update")
    @Operation(summary = "更新团款项目")
    @PreAuthorize("@ss.hasPermission('team:orde-payment:update')")
    public CommonResult<Boolean> updateOrdePayment(@Valid @RequestBody OrdePaymentSaveReqVO updateReqVO) {
        ordePaymentService.updateOrdePayment(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除团款项目")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('team:orde-payment:delete')")
    public CommonResult<Boolean> deleteOrdePayment(@RequestParam("id") Integer id) {
        ordePaymentService.deleteOrdePayment(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除团款项目")
                @PreAuthorize("@ss.hasPermission('team:orde-payment:delete')")
    public CommonResult<Boolean> deleteOrdePaymentList(@RequestParam("ids") List<Integer> ids) {
        ordePaymentService.deleteOrdePaymentListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得团款项目")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('team:orde-payment:query')")
    public CommonResult<OrdePaymentRespVO> getOrdePayment(@RequestParam("id") Integer id) {
        OrdePaymentDO ordePayment = ordePaymentService.getOrdePayment(id);
        return success(BeanUtils.toBean(ordePayment, OrdePaymentRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得团款项目分页")
    @PreAuthorize("@ss.hasPermission('team:orde-payment:query')")
    public CommonResult<PageResult<OrdePaymentRespVO>> getOrdePaymentPage(@Valid OrdePaymentPageReqVO pageReqVO) {
        PageResult<OrdePaymentDO> pageResult = ordePaymentService.getOrdePaymentPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, OrdePaymentRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出团款项目 Excel")
    @PreAuthorize("@ss.hasPermission('team:orde-payment:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportOrdePaymentExcel(@Valid OrdePaymentPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<OrdePaymentDO> list = ordePaymentService.getOrdePaymentPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "团款项目.xls", "数据", OrdePaymentRespVO.class,
                        BeanUtils.toBean(list, OrdePaymentRespVO.class));
    }

}